﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerChucVu: IChucVu
    {
        public ChucVu GetChucVu(int oid)
        {
            string sql =@"   SELECT MaQuanLy, TenChucVu 
                            FROM ChucVu 
                            Where Oid = @oid";

            SqlParameter param = new SqlParameter("@oid", oid);

            DataRow row = DataProvider.GetDataRow(sql, param);

            ChucVu chucVu = new ChucVu();
            chucVu.Oid = oid;
            chucVu.MaQuanLy = row["MaQuanLy"].ToString();
            chucVu.TenChucVu = row["TenChucVu"].ToString();

            return chucVu;
        }

        public List<ChucVu> GetListChucVu()
        {
            string sql = @" SELECT Oid, MaQuanLy, TenChucVu
                            FROM ChucVu";

            DataTable dt = DataProvider.GetDataTable(sql);

            List<ChucVu> list = new List<ChucVu>();

            foreach (DataRow row in dt.Rows)
            {
                ChucVu chucVu = new ChucVu();

                chucVu.Oid = int.Parse(row["Oid"].ToString());
                chucVu.MaQuanLy = row["MaQuanLy"].ToString();
                chucVu.TenChucVu = row["TenChucVu"].ToString();
                                
                list.Add(chucVu);
            }

            return list;
        }

        public int Insert(ChucVu chucVu)
        {
            string sql = @" Insert into ChucVu(MaQuanLy, TenChucVu)
                            Values(@MaQuanLy, @TenChucVu)";
            SqlParameter[] param = {
                                       new SqlParameter("@MaQuanLy", chucVu.MaQuanLy),
                                       new SqlParameter("TenChucVu", chucVu.TenChucVu)
                                   };
            DataProvider.Insert(sql, param);
            return 0;
        }

        public int Update(ChucVu chucVu)
        {
            string sql = @" Update ChucVu
                            Set MaQuanLy = @MaQuanLy,
                                TenChucVu = @TenChucVu
                            Where Oid = @Oid";
            SqlParameter[] param = {
                                       new SqlParameter("@Oid", chucVu.Oid),
                                       new SqlParameter("@MaQuanLy", chucVu.MaQuanLy),
                                       new SqlParameter("TenChucVu", chucVu.TenChucVu)
                                   };
            return DataProvider.Update(sql, param);
        }

        public int Delete(ChucVu chucVu)
        {
            string sql = @" Delete From ChucVu Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", chucVu.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(ChucVu obj)
        {
            throw new NotImplementedException();
        }
    }
}
